﻿﻿<?php
ob_start();
header("Content-type:text/html;charset=utf-8");
ini_set('memory_limit','88M');
ini_set('max_execution_time','0');
date_default_timezone_set('Asia/Shanghai');
error_reporting(0);
$date   = date('Y-m-d');
$dateex = explode('-',$date);
$stime  = time();
$stime1 = mktime('0','0','10',$dateex[1],$dateex[2],$dateex[0]);
$stime2 = mktime('23','59','59',$dateex[1],$dateex[2],$dateex[0]);
if($stime>=$stime1 && $stime<=$stime2){
}else{
    echo "fuck you mom!!!get out(return money)!! \n";
    exit;
}


include 'config.php';

/*----------链接mysql数据库  start------------*/
$conn = @mysql_connect($db_host,$db_user,$db_pwd) or die('mysql server failed!!');
@mysql_query('set names utf8',$conn);
@mysql_select_db($db_name,$conn) or die('can not find mysql dbname:'.$db_name);
/*----------链接mysql数据库  end------------*/

/*----------常用方法封装  start------------*/
function querys($sql){
    return @mysql_query($sql);
}

function getall($sql){
    $query = querys($sql);
    $list = '';
    while($row = @mysql_fetch_assoc($query)){
        $list[] = $row;
    }
    return $list;
}

function getone($sql){
    $query = querys($sql);
    return @mysql_fetch_assoc($query);
}
/*
*修改实例
*$table=表名
*$err=数组
*$id=编号
*/
function edit_update_cl($table,$err,$id){
    $sql = "UPDATE ".$table." SET ";
    foreach ( $err as $key => $value  ){
        if(preg_match("/^\d*$/",$key)){
            #过滤数字
        }else{
            $sql .= $key."='".addslashes($value)."', ";
        }
    }
    $sql = substr($sql, 0, strlen($sql)-2) .  " WHERE id=".$id;

    @mysql_query($sql) or die (mysql_error());

}

/*
*插入实例
*$table=表名
*$err=数组
*/
function add_insert_cl($table,$err){
    $sql = "INSERT INTO ".$table." (";
    foreach ( $err as $key => $value  ){
        $sql .= $key.", ";
    }
    $sql = substr($sql, 0, strlen($sql)-2) .  ") VALUES (";
    foreach ( $err as $key => $value  ){
        $sql .= "'".addslashes($value)."', ";
    }
    $sql = substr($sql, 0, strlen($sql)-2) .  ")";
    @mysql_query($sql) or die (mysql_error());
    return mysql_insert_id();
}

$sys=getone("select * from clt_system where id=1");
$xingqi = date("w");
if($sys['jingtai']=='close'){
    echo "静态已关闭";
    exit;
}
$timeday=strtotime(date("Y-m-d"));
if($timeday<=$sys['day_time_jc']){
    echo "当日已发放";
    exit;
}



$time=unserialize($sys['jingtai_ff']);
if(in_array($xingqi, $time) || in_array(8, $time)) {
}else {
    echo "当前日期不能发放";
    exit;
}

/*修改或者新增bonus表*/
function edit_add_bonus($uid,$base_mey_per){
    /*修改bonus表,先查一下今天有没有数据*/
    $td_time = strtotime(date('Y-m-d'));
    $sql_check_bouns = "select * from clt_bonus where uid='".$uid."' and `createtime`>='".$td_time."'";
    $sql_check_bouns = getone($sql_check_bouns);
    if(!empty($sql_check_bouns['id'])){
        //如果有数据，直接修改对应的列
        $sql_bonus = "update clt_bonus set `b1`=(b1+$base_mey_per),`b0`=(b0+$base_mey_per) where id='".$sql_check_bouns['id']."'";
    }else{
        $sql_bonus = "insert into clt_bonus (is_type,uid,b0,b1,createtime) values(0,'".$uid."',$base_mey_per,$base_mey_per,'".time()."');";
    }
    querys($sql_bonus);
}

//统计每日奖金（日封顶基础）
function bonus_insert($b0,$uid,$b1,$b2,$b3,$b4,$b5,$b6,$b7=0,$b8=0,$b9=0,$b10=0,$b11=0,$b12=0,$b13=0) {
    $date=strtotime( date("Y-m-d"));
    $sql_bonus="select * from clt_bonus where createtime>".$date." and uid=".$uid;
    $bonus=getone($sql_bonus);
    if (!empty($bonus)){
        $bonus_update = NULL;
        $bonus_update ['b0'] = $bonus ['b0'] + $b0;
        $bonus_update ['b1'] = $bonus ['b1'] + $b1;
        $bonus_update ['b2'] = $bonus ['b2'] + $b2;
        $bonus_update ['b3'] = $bonus ['b3'] + $b3;
        $bonus_update ['b4'] = $bonus ['b4'] + $b4;//积分
        $bonus_update ['b5'] = $bonus ['b5'] + $b5;
        $bonus_update ['b6'] = $bonus ['b6'] + $b6;
        $bonus_update ['b7'] = $bonus ['b7'] + $b7;
        $bonus_update ['b8'] = $bonus ['b8'] + $b8;
        $bonus_update ['b9'] = $bonus ['b9'] + $b9;
        $bonus_update ['b10'] = $bonus ['b10'] + $b10;
        $bonus_update ['b11'] = $bonus ['b11'] + $b11;
        $bonus_update ['b12'] = $bonus ['b12'] + $b12;
        $bonus_update ['b13'] = $bonus ['b13'] + $b13;
        edit_update_cl ( 'clt_bonus', $bonus_update, $bonus ['id'] );

        /*	db('bonus')->where('id',$bonus['id'])->update($bonus_update);*/
        //edit_update_cl ( 'bonus', $bonus_update, $bonus ['id'] );
    } else {
        $bonus_update = NULL;
        $bonus_update ['createtime'] = time();
        $bonus_update ['uid'] = $uid;
        $bonus_update ['b0'] = $b0;
        $bonus_update ['b1'] = $b1;
        $bonus_update ['b2'] = $b2;
        $bonus_update ['b3'] = $b3;
        $bonus_update ['b4'] = $b4;
        $bonus_update ['b5'] = $b5;
        $bonus_update ['b6'] = $b6;
        $bonus_update ['b7'] = $b7;
        $bonus_update ['b8'] = $b8;
        $bonus_update ['b9'] = $b9;
        $bonus_update ['b10'] = $b10;
        $bonus_update ['b11'] = $b11;
        $bonus_update ['b12'] = $b12;
        $bonus_update ['b13'] = $b13;
        add_insert_cl ( 'clt_bonus', $bonus_update );
        //	db('bonus')->insert($bonus_update);
    }

}

//业绩统计
function achievement($a,$b,$c,$d,$e,$f,$g,$h,$i,$j=0,$k=0,$l=0,$m=0,$n=0){
    $createtime=strtotime(date('Y-m-d'));

    $achievement=getone("select * from clt_total_achievement where createtime=".$createtime." ");
    if(!empty($achievement)){
        $achievement_change['createtime']=$createtime;
        $achievement_change['newadd_achievement']=$achievement['newadd_achievement']+$a;
        $achievement_change['all_achievement']=$achievement['all_achievement']+$b;
        $achievement_change['newadd_user']=$achievement['newadd_user']+$c;
        $achievement_change['all_user']=$achievement['all_user']+$d;
        $achievement_change['new_grant']=$achievement['new_grant']+$e;
        $achievement_change['all_grant']=$achievement['all_grant']+$f;
        $achievement_change['bonus1']=$achievement['bonus1']+$g;
        $achievement_change['bonus2']=$achievement['bonus2']+$h;
        $achievement_change['bonus3']=$achievement['bonus3']+$i;
        $achievement_change['bonus4']=$achievement['bonus4']+$j;
        $achievement_change['bonus5']=$achievement['bonus5']+$k;
        $achievement_change['bonus6']=$achievement['bonus6']+$l;
        $achievement_change['bonus7']=$achievement['bonus7']+$m;
        $achievement_change['bonus8']=$achievement['bonus8']+$n;
        edit_update_cl ( 'clt_total_achievement', $achievement_change, $achievement['id'] );
        //	db('total_achievement')->where('id',$achievement['id'])->update($achievement_change);
    }else{
        $all_achievement=querys("select all_achievement from clt_total_achievement where id>0 order by id desc limit 0,1");
        $all_user=querys("select all_user from clt_total_achievement where id>0 order by id desc limit 0,1");
        $all_grant=querys("select all_grant from clt_total_achievement where id>0 order by id desc limit 0,1");
        $achievement_change['createtime']=$createtime;
        $achievement_change['newadd_achievement']=$a;
        $achievement_change['all_achievement']=$b+$all_achievement['all_achievement'];
        $achievement_change['newadd_user']=$c;
        $achievement_change['all_user']=$d+$all_user['all_user'];
        $achievement_change['new_grant']=$e;
        $achievement_change['all_grant']=$f+$all_grant['all_grant'];
        $achievement_change['bonus1']=$g;
        $achievement_change['bonus2']=$h;
        $achievement_change['bonus3']=$i;
        $achievement_change['bonus4']=$j;
        $achievement_change['bonus5']=$k;
        $achievement_change['bonus6']=$l;
        $achievement_change['bonus7']=$m;
        $achievement_change['bonus8']=$n;
        add_insert_cl ( 'clt_total_achievement', $achievement_change );
        //	db('total_achievement')->where('id',$achievement['id'])->insert($achievement_change);
    }
}

function money_log($id,$money,$beizhu,$jifen,$source){
    //增加积分变动日志
    $addinfo['type']=1;
    $addinfo['userid']=$id;
    $addinfo['dangqian']=$money+$jifen;
    $addinfo['num']="+".$money;
    $addinfo['source']=$source;
    $addinfo['time']=time();
    if(!empty($beizhu)){
        $addinfo['beizhu']=$beizhu;

    }
    add_insert_cl ( 'clt_money_log', $addinfo );
}

/*
function lirunjiang($rpath,$centerid,$id,$tlevel_user){
    $rpath=trim($rpath,",");
    $rpatharr=explode(",",$rpath);
    $path=array_reverse($rpatharr);
    $a=0;
    $b=0;
    $c=0;
    $d=0;
    $e=0;
    $f=0;
    $g=0;
    foreach ($path as $key=>$value){
        $money=0;
        $vuser = getone("select centerid,`level`,`tlevel`  from clt_users where id=".$value);
        $sql_level="select b2 from clt_user_tlevel where `level`=".$vuser['tlevel'];
        $tlevel=getone($sql_level);
        if($vuser['tlevel']==2&&$vuser['tlevel']>$tlevel_user){

            if($b==0&&$c==0&&$d==0&&$e==0&&$f==0&&$g==0){
                if($a==0){
                    $money=$level['lirun'];
                }else{
                    $level2=getulevel($vuser['level']-1);
                    $money=$level['lirun']-$level2['lirun'];
                }
                $b=1;
            }
            if($b==0&&$c==0&&$d==0&&$e==0&&$f==0&&$g==0){
                if(in_array($vuser['rightid'],$path)){
                    $money=$vuser['centerid'];
                }else{
                    $money=$vuser['centerid']-$centerid;
                    $cha=$vuser['centerid']-$money;
                    $meycha=$centerid-$cha;
                    querys("update clt_users set narea=".$meycha." where id=".$id);
                }
                $b=1;
            }else{
                $money=$vuser['centerid']-$centerid;
                $cha=$vuser['centerid']-$money;
                $meycha=$centerid-$cha;
                querys("update clt_users set narea=".$meycha." where id=".$id);
            }
        }elseif($vuser['tlevel']==3&&$vuser['tlevel']>$tlevel_user){
            if($c==0&&$d==0&&$e==0&&$f==0&&$g==0){
                if($b==1){
                    $sql_level3="select b2 from clt_user_tlevel where `level`=".$vuser['tlevel']-1;
                    $tlevel3=getone($sql_level3);
                    $money=($tlevel['b2']-$tlevel3['b2'])/1000;
                }elseif($a==1&&$b==0){
                    $sql_level1="select b2 from clt_user_tlevel where `level`=".$vuser['tlevel']-2;
                    $tlevel1=getone($sql_level1);
                    $money=($tlevel['b2']-$tlevel1['b2'])/1000;
                }else{
                    $money=$tlevel['b2']/1000;
                }
                $c=1;
            }
        }elseif($vuser['level']==4&&$d==0&&$e==0&&$f==0&&$g==0){
            if($c==1){
                $level2=getulevel($vuser['level']-1);
                $money=$level['lirun']-$level2['lirun'];
            }elseif($b==1){
                $level2=getulevel($vuser['level']-2);
                $money=$level['lirun']-$level2['lirun'];
            }elseif($a==1){
                $level2=getulevel($vuser['level']-3);
                $money=$level['lirun']-$level2['lirun'];
            }else{
                $money=$level['lirun'];
            }
            $d=1;
        }
        if($money>0){
            $mey=$money;
            querys("update clt_users set leftid=leftid+".$mey ." where id=".$value);
        }
    }
}*/

/*----------常用方法封装  end------------*/
$time=strtotime(date("Y-m-d 0:0:0"));
//首先先获取需要执行静态的数据
$sql="select rpath,mobile,id from clt_users where  tlevel>0 order by id desc ";
$list = getall($sql);
if(empty($list)){
    echo '暂无需要执行静态的数据';
    exit;
}
/*
*	静态奖
*/
$sys=getone("select id,achievement from clt_system where id=1");
foreach($list as $k=>$v){
    $achi_sql=getone("select achievement,id from clt_users where up_userid=".$v['id']." order by achievement desc limit 0,1");
    $cha=$v['achievement']-$achi_sql['achievement'];
    $sql_level="select b2,b1 from clt_user_tlevel where `level`=".$vuser['tlevel'];
    $tlevel=getone($sql_level);
    $mey=($cha-$tlevel['b1'])/100000*$tlevel['b2'];
    querys("update clt_users set leftid=".$mey." where id=".$v['id']);
}

/*$list_sql="select rpath,centerid from clt_users where centerid>0";
$tlevel_arr=getall($list_sql);
foreach ($tlevel_arr as $k=>$v){
    lirunjiang($v['rpath'],$v['centerid'],$v['id'],$v['tlevel']);
}*/


//跟新当日发放时间
echo '发放成功';
?>



